package com.question;

import com.until.Testcutdownlatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*实现一个容器，提供两个方法，add，size
        写两个线程，线程1添加10个元素到容器中，线程2实现监控元素的个数，当个数到5个时，线程2给出提示并结束*/
public class Test3 {


    public  static  void  main(String []args)
    {

         final Thread thread1= new Thread(new Runnable() {
            public void run() {
                //for (int i=0;i<10;i++)
                try {
                    while (true)
                    {
                        System.out.println(Thread.currentThread().getName()+"is running");


                        if(Thread.currentThread().isInterrupted())
                        {
                            System.out.println(Thread.currentThread().getName()+"interrupt，即将退出");
                            throw new InterruptedException();
                        }
                        Thread.sleep(1000);

                    }
                } catch (InterruptedException e) {
                    System.out.println("进入ExceptionInterrupt2 类中run方法的catch了!");
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"is ending");


            }
        });
         thread1.start();
        new Thread(new Runnable() {
            public void run() {

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //System.out.println("到达5个，结束线程");
                thread1.interrupt();
                //System.out.println("到达5个，结束线程");

            }
        }).start();
        //service.shutdown();

    }


}
 class DataAndProcess3{


    public synchronized void add(int i)
    {
        if (i>=5)
        {
            this.notify();
            Thread.interrupted();
        }
        System.out.println(Thread.currentThread().getName()+"add"+i);

    }
    public  synchronized void size()
    {
        try {
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("到达5个，结束线程");

    }
}
